package com.weirblog.util;

import javax.servlet.http.HttpServletRequest;

/**
 * IP工具类
 * 
 */
public class IpUtil {
	
	/**
	 * 获取登录用户的IP地址
	 * 
	 * @param request
	 * @return
	 */
	public static String getIpAddr(HttpServletRequest request) {
        String ip = null;
        try {
            ip = request.getHeader("x-forwarded-for");
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("Proxy-Client-IP");
            }
            if (ip == null || ip.length() == 0 || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("WL-Proxy-Client-IP");
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_CLIENT_IP");
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_X_FORWARDED_FOR");
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getRemoteHost();
            }
            return ip;
        } catch (Exception e) {
            return ip;
    }

}

	/**
	 * 通过IP获取地址(需要联网，调用淘宝的IP库)
	 * 
	 * @param ip
	 * @return
	 */
//	public static String getIpInfo(String ip) {
//		if (ip.equals("本地")) {
//			ip = "127.0.0.1";
//		}
//		String info = "";
//		try {
//			URL url = new URL("http://ip.taobao.com/service/getIpInfo.php?ip=" + ip);
//			HttpURLConnection htpcon = (HttpURLConnection) url.openConnection();
//			htpcon.setRequestMethod("GET");
//			htpcon.setDoOutput(true);
//			htpcon.setDoInput(true);
//			htpcon.setUseCaches(false);
//
//			InputStream in = htpcon.getInputStream();
//			BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));
//			StringBuffer temp = new StringBuffer();
//			String line = bufferedReader.readLine();
//			while (line != null) {
//				temp.append(line).append("\r\n");
//				line = bufferedReader.readLine();
//			}
//			bufferedReader.close();
////			JSONObject obj = (JSONObject) JSON.parse(temp.toString());
//			JsonVO<IpInfoVO> jsonVO = mapper.readValue(temp.toString(), JsonVO.class);
////			Map readValue = mapper.readValue(temp.toString(), Map.class);
//			System.out.println(jsonVO.toString());
////			if (obj.getIntValue("code") == 0) {
////				JSONObject data = obj.getJSONObject("data");
////				info += data.getString("country") + " ";
////				info += data.getString("region") + " ";
////				info += data.getString("city") + " ";
////				info += data.getString("isp");
////			}
//		} catch (MalformedURLException e) {
//			e.printStackTrace();
//		} catch (ProtocolException e) {
//			e.printStackTrace();
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
//		return info;
//	}
}
